package ma.myschool.app.webapp.action;

import java.io.Serializable;
import java.util.List;

import ma.myschool.app.dao.SearchException;
import ma.myschool.app.model.Groupe;
import ma.myschool.app.service.GenericManager;
import ma.myschool.app.webapp.action.BasePage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("groupeList")
@Scope("session")
public class GroupeList extends BasePage implements Serializable {
    private String query;
    private GenericManager<Groupe, Long> groupeManager;

    @Autowired
    public void setGroupeManager(@Qualifier("groupeManager") GenericManager<Groupe, Long> groupeManager) {
        this.groupeManager = groupeManager;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getQuery() {
        return query;
    }

    public GroupeList() {
        setSortColumn("id"); // sets the default sort column
    }

    public List<Groupe> getGroupes() {
        try {
            return groupeManager.search(query, Groupe.class);
        } catch (SearchException se) {
            addError(se.getMessage());
            return sort(groupeManager.getAll());
        }
    }
    
    public int getGroupesSize(){
    	return getGroupes().size();
    }


    public String search() {
        return "success";
    }
}